package edu.hubu.shop9.controller;

import edu.hubu.shop9.entity.Employee;
import edu.hubu.shop9.service.IDepartmentService;
import edu.hubu.shop9.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;


/**
 * @author waxijiang
 */
@Controller
public class MainController {
    @Autowired
    private IEmployeeService employeeService;
    private IDepartmentService departmentService;

    @RequestMapping({"/main", "/"})
    public String main() {
        return "main";
    }

    @RequestMapping("/loginPage")
    public String loginPage() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, String username, String password) {
        Employee employee = employeeService.checkLogin(username, password);
        if (employee != null) {
            request.getSession().setAttribute("USER_NAME", username);
            request.getSession().setAttribute("avatar", employee.getHeadimage());
            return "redirect:/main";
        } else {
            request.setAttribute("msg", "账号或者密码错误!");
            return "login";
        }
    }


    @RequestMapping("/loginout")
    public String loginout(HttpServletRequest request) {
        request.getSession().removeAttribute("USER_NAME");
        return "redirect:/loginPage";
    }
}
